package Client; //done

import GUI.LoginMenu;

import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class the initialize port and ip for client
 * @author waleed
 * @author kamel
 * @author bshara
 * @author ameer
 */
public class ClientLoginWindow extends JFrame{

	/** The serial Version UID. */
	private static final long serialVersionUID = 1L;
	
	/** The Port Label. */
	private JLabel portLabel = null;
	
	/** The IP Label. */
	private JLabel ipLabel = null;
	
	/** The IP Text Field. */
	private JTextField ipField = null;
	
	/** The Port Text Field. */
	private JTextField portLabelField = null;
	
	/** The Start Button. */
	private JButton startButton = null;
	
	/** The ContentPane JPanel. */
	private JPanel jContentPane = null;
	
	/** The Background Label. */
	private JLabel BackgroundLabel;
	
	/**
	 * Default constructor
	 */
	public ClientLoginWindow() {
		super();
		initialize();		
	}

	/**
	 * This function initializes LoginForm 
	 */
	private void initialize()
	{
		this.setSize(400, 300);
		this.setContentPane(getJContentPane());
		this.setTitle("CCRM CLIENT");		
	}
	
	/**
	 * This function initializes JContentPane
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			portLabel  = new JLabel();
			portLabel .setBounds(new Rectangle(55, 64, 113, 22));
			portLabel .setText("Server Port:");
			
			ipLabel  = new JLabel();
			ipLabel .setBounds(new Rectangle(55, 31, 113, 22));
			ipLabel .setText("Server IP Address:");
						
			jContentPane.add(portLabel, null);
			jContentPane.add(ipLabel, null);
			jContentPane.add(getPortLabelField(), null);
			jContentPane.add(ipField(), null);
			jContentPane.add(getStartButton(),null);
			
			JLabel label = new JLabel("");
			label.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/login.png")));
			label.setBounds(145, 99, 143, 85);
			jContentPane.add(label);
			
			BackgroundLabel = new JLabel("");
			BackgroundLabel.setIcon(new ImageIcon(LoginMenu.class.getResource("/Pictures/bg.png")));
			BackgroundLabel.setBounds(0, 0, 400, 300);
			jContentPane.add(BackgroundLabel);
		}
		return jContentPane;		
	}
	
	/**
	 * This function initializes portLabelField	
	 */
	private JTextField getPortLabelField(){
		if (portLabelField == null){
			portLabelField = new JTextField();
			portLabelField.setBounds(new Rectangle(170, 64, 130, 22));			
		}
		return portLabelField;
	}
	
	
	/**
	 * This function initializes ipField	
	 */
	private JTextField ipField(){
		if (ipField == null) {
			ipField = new JTextField();
			ipField.setBounds(new Rectangle(170, 31, 130, 22));			
		}
		return ipField;
	}
	
	
	/**
	 * This function initializes startButton	
	 */
	public JButton getStartButton(){
		if (startButton == null){
			startButton = new JButton();
			startButton.setBounds(new Rectangle(124, 195, 142, 29));
			startButton.setText("START");
		}	
		return startButton;
	}	
	 
	/**
	 *  This function return the entered port	
	 */
	public int getPort(){
		int port=0;
		String msg;
		try{
			if (portLabelField != null){
				msg = portLabelField.getText();
				if (msg.equals(""))
					return 0;
				port = Integer.parseInt(msg);
			}	
			return port;
		}
		catch (Exception e){
			return -1;
		}		
	}
	
	/**
	 *  This function return the entered IP	
	 */
	public String getIP(){
		String ip = "";
		if (ipField != null){
			ip = ipField.getText();
			if (ip.equals("")){
				return "localhost";
			}
			else
				return ip;			
		}	
		return "localhost";
	}
}



